//
//  ViewController.m
//  电影
//
//  Created by dllo on 16/6/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "MovieModel.h"
#import "CustomTableViewCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *movieArray;

@property (nonatomic, retain) UITableView *tableView;

@end

@implementation ViewController

-(void)dealloc
{
    [_tableView release];
    [_movieArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    
    [self handleData];
    
    [self createTableView];

}

-(void)config
{
    self.movieArray = [NSMutableArray array];
    
    self.navigationItem.title = @"电影";
    
    self.view.backgroundColor = [UIColor colorWithRed:0.99 green:0.92 blue:0.94 alpha:1.0];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationItem.rightBarButtonItem = [self editButtonItem];
}


#pragma mark - UITableView编辑
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    /**让tableView处于编辑状态*/
    [self.tableView setEditing:editing animated:animated];
}


/**设定那些行可以编辑那些行不可以编辑*/
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

/**设置编辑风格*/
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2 == 0) {
        return UITableViewCellEditingStyleDelete;
    }else{
        return UITableViewCellEditingStyleInsert;
    }
}

/**提交编辑风格,同时提交数据*/
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (editingStyle) {
        case UITableViewCellEditingStyleDelete:
        {
            [self.movieArray removeObjectAtIndex:indexPath.row];
            if (self.movieArray.count == 0) {
                tableView.allowsSelectionDuringEditing = NO;
            }
        }
            break;
        case UITableViewCellEditingStyleInsert:
        {
            MovieModel *movie = [[MovieModel alloc]init];
            movie.movieName = @"谍影重重";
            movie.movieId = @"2012";
            
            [self.movieArray insertObject:movie atIndex:indexPath.row];
            
            [tableView reloadData];
        }
            break;
        default:
            break;
    }
}

#pragma mark - 移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    MovieModel *movie = [self.movieArray objectAtIndex:sourceIndexPath.row];
    
    [movie retain];
    
    [self.movieArray removeObjectAtIndex:sourceIndexPath.row];
    
    [self.movieArray insertObject:movie atIndex:destinationIndexPath.row];
    
    [movie release];
    
    [self.tableView reloadData];
    
}

#pragma mark -
/**数据处理*/
-(void)handleData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movielist" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *array = [dic objectForKey:@"result"];
    
    for (NSDictionary *dic in array) {
        
        MovieModel *movie = [[MovieModel alloc]init];
        
        [movie setValuesForKeysWithDictionary:dic];
        
        [self.movieArray addObject:movie];
    }
//    NSLog(@"%@", self.movieArray);
    
}

-(void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 64) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];

    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    self.tableView.rowHeight = 80;
    
    [self.tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"cell"];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.cellModel = [self.movieArray objectAtIndex:indexPath.row];
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.movieArray.count;
}

@end





















